import HttpMethod from "../../shared/constants/http-method-constant";
import Token from "../../shared/models/token";
import AuthenticationService from "../../shared/services/authentication-service";
import HttpRequestService from "../../shared/services/http-request-service";
import TokenService from "../../shared/services/token-service";
import AuthService from "./auth-service";

Page({
  data: {
    jsonData: [],
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs',
    });
  },
  onLoad() {
    this.getJson()
  },
  async login(): Promise<void> {
    // if (!isAuthenticated) {
    const authCode = await AuthService.sendAuthCode({
      username: '17702197245',
      type: 'AUTHENTICATION',
    });

    const res = await AuthService.login('17702197245', authCode);

    if (res?.accessToken) {
      // Gets the server data to user instance.
      // const user = User.fromJson(res);

      // Saves the user data retrieved from server.
      AuthenticationService.saveUserInfo(res);

      // Gets the server data to token instance.
      const token = await Token.fromServerJson(res);

      // Saves the token data retrieved from server.
      TokenService.saveToken(token);
    }
    // } else {
    //   AuthenticationService.removeUserInfo();
    //   TokenService.removeToken();
    // }

    // dispatch(change(AuthenticationService.isAuthenticated));
  },
  async testApi(): Promise<void> {
    let i = 0

    while (i < 10) {
      const res = await AuthService.testApi({ pageIndex: 0, pageSize: 2 });

      i++;
    }
  },
  // async test(): Promise<void> {
  //   const res = await HttpRequestService.post('/user/portal/get', {
  //     padId: '3C063C17E6EA478E980EC3AE9DD3D6A9',
  //   });

  //   const defs = DynamicElementDefinitionFactory.getDefinitions(res.payload.data.padCont);

  //   this.setData({
  //     html: defs[4].config
  //   })

  //   console.info(await DynamicElementDataFactory.queryData(defs[4]))
  //   console.info(defs);
  // },
  test: function (event: any) {
    wx.navigateTo({
      url: `/pages/dynamic-page/dynamic-page?padId=${event.target.dataset.padid}`
    })
  },

  getJson: async function () {
    const jsonData = (await HttpRequestService.makeOriginalRequest(HttpMethod.get, 'https://www.wenlu.net.cn/sf-test/smart-farm-resources/dynamic-page/page-list.json')).payload;

    this.setData({
      jsonData: jsonData
    })
  }
});